package personal.jinhui.paprika.ui;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import personal.jinhui.paprika.PaprikaService;
import personal.jinhui.paprika.R;
import personal.jinhui.paprika.service.aidl.IXmppFacade;

public class BaiduLocationActivity extends Activity {

	private static final String TAG = "BaiduLocationActivity";

	private LocationClient mLocationClient = null;
	private Button mStartBtn = null;
	private TextView mTextView = null;
	private Button mPubLocation;
	private Button mSubLocation;
	private double mLng;
	private double mLat;

	private IXmppFacade mXmppFacade;
	private final ServiceConnection mServConn = new PaprikaServiceConnection();
	private boolean mBinded;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.baidu_location);

		mTextView = (TextView) findViewById(R.id.textview1);
		mStartBtn = (Button) findViewById(R.id.button1);
		mPubLocation = (Button) findViewById(R.id.button2);
		mSubLocation = (Button) findViewById(R.id.button3);

		mLocationClient = new LocationClient(this);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // ��gps
		option.setCoorType("bd09ll"); // �����������Ϊbd09ll
		option.setPriority(LocationClientOption.NetWorkFirst); // ������������
		option.setProdName("locSDKDemo2"); // ���ò�Ʒ�����
		option.setScanSpan(5000); // ��ʱ��λ��ÿ��5���Ӷ�λһ�Ρ�
		mLocationClient.setLocOption(option);
		mLocationClient.registerLocationListener(new BDLocationListener() {
			@Override
			public void onReceiveLocation(BDLocation location) {
				if (location == null)
					return;
				StringBuffer sb = new StringBuffer(256);
				sb.append("time : ");
				sb.append(location.getTime());
				sb.append("\nerror code : ");
				sb.append(location.getLocType());
				sb.append("\nlatitude : ");
				sb.append(location.getLatitude());
				mLat = location.getLatitude();
				sb.append("\nlontitude : ");
				sb.append(location.getLongitude());
				mLng = location.getLongitude();

				sb.append("\nradius : ");
				sb.append(location.getRadius());
				if (location.getLocType() == BDLocation.TypeGpsLocation) {
					sb.append("\nspeed : ");
					sb.append(location.getSpeed());
					sb.append("\nsatellite : ");
					sb.append(location.getSatelliteNumber());
				} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
					sb.append("\naddr : ");
					sb.append(location.getAddrStr());
				}
				sb.append("\nsdk version : ");
				sb.append(mLocationClient.getVersion());
				mTextView.setText(sb.toString());
			}

			public void onReceivePoi(BDLocation location) {
				// return ;
			}
		});

		mStartBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mLocationClient == null)
					return;
				if (mLocationClient.isStarted()) {
					mLocationClient.stop();
					mStartBtn.setText("start");
				} else {
					mLocationClient.start();
					mStartBtn.setText("stop");
				}
			}
		});

	}

	@Override
	public void onDestroy() {
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
			mLocationClient = null;
		}
		super.onDestroy();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onResume() {
		super.onResume();
		bindService(new Intent(this, PaprikaService.class), mServConn,
				BIND_AUTO_CREATE);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onPause() {
		super.onPause();
		unbindService(mServConn);
	}

	/**
	 * The service connection used to connect to the Paprika service.
	 */
	private class PaprikaServiceConnection implements ServiceConnection {

		/**
		 * Constructor.
		 */
		public PaprikaServiceConnection() {
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mXmppFacade = IXmppFacade.Stub.asInterface(service);
			mPubLocation.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					try {
						String data = mLng + "," + mLat;
						mXmppFacade.publishLocation(data);
					} catch (RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			});
			mSubLocation.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					/*
					 * try { mXmppFacade.subscribeLocation(); } catch
					 * (RemoteException e) { // TODO Auto-generated catch block
					 * e.printStackTrace(); }
					 */

				}
			});
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mXmppFacade = null;
			mBinded = false;
		}
	}

}